/* 
 * File:   NewStackCommand.cpp
 * Author: Gab'z
 * 
 * Created on 5 juin 2012, 02:35
 */

#include "NewStackCommand.h"
#include "../../Context.h"
#include "../../command/literal/StackList.h"
#include "../../memento/CurrentStackMemento.h"
#include "../../../exception/MementoException.h"

namespace calculator {

    NewStackCommand::NewStackCommand(const std::string name) : StackListCommand(name) {
    }

    NewStackCommand::NewStackCommand(const NewStackCommand& orig) : StackListCommand(orig._name) {
    }

    NewStackCommand::~NewStackCommand() {
    }

    NewStackCommand* NewStackCommand::clone() const {
        return new NewStackCommand(_name);
    }

    std::string NewStackCommand::isExecutable() const {
        return "";
    }

    const Memento* NewStackCommand::createMemento() const throw (CommandException) {
        return new CurrentStackMemento(clone(), Context::getInstance()->getStackList()->getCurrentStackIndex());
    }

    void NewStackCommand::restoreFromMemento(const Memento* memento) const throw (MementoException) {
        const CurrentStackMemento* currentStackMemento = dynamic_cast<const CurrentStackMemento*> (memento);
        if (currentStackMemento == 0) {
            std::string str = "Bad Memento type, expected : CurrentStackMemento, was ";
            str += typeid (*memento).name();
            throw MementoException(str);
        }
        Context::getInstance()->getStackList()->removeCurrentStack();
        Context::getInstance()->getStackList()->setCurrentStack(currentStackMemento->_stackIndex);
    }

    void NewStackCommand::apply(StackList* stacklist) const throw (CommandException) {
        stacklist->newStack();
    }
}